################################################################################-
# Replication File for Wratil, Wäckerle and Proksch: Government Rhetoric and the 
# Representation of Public Opinion in International Negotiations
#
# This script runs the analysis presented in Appendix I5 
# (Bootstrapping Procedure for Clustered Standard Errors).
#
# Rerunning this script will result in slightly different results, 
# because we don't set a seed.
#
# Additionally, the script produces the following graphs and tables:
# Figure I5
################################################################################-

library(tidyverse) # version 1.3.2
library(ggpubr)    # version 0.4.0
library(stm)       # version 1.3.6
source("99_functions.R")

# If you change this to "yes", this will take a long time, for most machines multiple days.
rerun_simulations <- "no"
##################################-
#### Simulations new function ####

if(rerun_simulations == "yes"){
  results_simulations <- data.frame(sim = rep(seq(from=5,to = 500,by = 5),3)) %>% 
    bind_rows(data.frame(sim = rep(seq(from=525,to = 1500,by = 10),2))) %>% 
    bind_rows(data.frame(sim = rep(seq(from=1500,to = 10000,by = 1000),2))) %>% 
    arrange(sim)
  results_simulations$num <- 1:nrow(results_simulations)
  results_simulations$std.error.2 <- NA
  results_simulations$std.error.1 <- NA
  
  list_simulations <- list()
  load("generated_data/dfm_for_stm.RData")
  
  data_dfm_main <- data_dfm
  docvars(data_dfm_main) <- docvars(data_dfm_main) %>% 
    data.frame() %>% 
    select(gov_eu_supporter,image_lag6m,date_correct,Transcription,
           part_of_speech,text_copy,text_original,Actor,
           gov_lr_cmp_static,eu_cont_gdp,budget_any,unanimity_any,
           unemployment,inflation,north_south,
           Negotiation.Stage.stages,Council_Config_final)
  data_dfm_main <- dfm_subset(data_dfm_main,complete.cases(docvars(data_dfm_main)))
  
  load(file="generated_data/stm_out_40_main_model.RData") 
  data.sum <- docvars(data_dfm_main)
  
  for(i in 1:nrow(results_simulations)){
    if(is.na(results_simulations$std.error.2[i])){
      prep_council_40_save <- estimateEffect_cluster(c(40) ~ gov_eu_supporter*image_lag6m+
                                                       part_of_speech+
                                                       gov_lr_cmp_static+eu_cont_gdp+budget_any+unanimity_any+
                                                       unemployment+inflation+north_south+
                                                       Negotiation.Stage.stages+Council_Config_final,
                                             stm_out_40_main_model,nsims=results_simulations$sim[i],
                                             meta = docvars(data_dfm_main), uncertainty = "Global")
      
      print("Calculating significance for interacted variables...")
      tab_40 <- calc_interaction_regression_stm(var_1 = "image_lag6m",
                                                var_mod = "gov_eu_supporter",
                                                topic = 40,
                                                estimate_object = prep_council_40_save)
      
      list_simulations[[i]] <- tab_40
    }
    
    
    print(paste("Simulation",i,"of",nrow(results_simulations)))

  }
  save(file="generated_data/simulations_cluster_function.RData",results_simulations,list_simulations)
}else{
  load(file="generated_data/simulations_cluster_function.RData")
}

for(l in 1:nrow(results_simulations)){
  if(l <= length(list_simulations)){
    dat.sav <- list_simulations[[l]]
    results_simulations$est.1[l] <- dat.sav$Estimate[1]
    results_simulations$est.2[l] <- dat.sav$Estimate[2]
    results_simulations$std.error.1[l] <- dat.sav$Std.Error[1]
    results_simulations$std.error.2[l] <- dat.sav$Std.Error[2]
    results_simulations$t.val.1[l] <- dat.sav$tval[1]
    results_simulations$t.val.2[l] <- dat.sav$tval[2]
  }
  
}

plot_philes <- ggplot(results_simulations %>% filter(!is.na(std.error.2)),aes(x=sim,y=t.val.1))+
  geom_point()+
  theme_minimal()+
  labs(title="Pro-EU Governments",
       y = "t-statistic",
       x = "# of Resampled Draws from \nVariational Posterior")

plot_sceptics <- ggplot(results_simulations %>% filter(!is.na(std.error.2)),aes(x=sim,y=t.val.2))+
  geom_point()+
  theme_minimal()+
  labs(title="Eurosceptic Governments",
       y = "t-statistic",
       x = "# of Resampled Draws from \nVariational Posterior")

# This is Figure I5
ggarrange(plot_philes,plot_sceptics)
ggsave("figures_appendix/figure_i_5.eps", width = 6, height = 3, units = "in")
